package com.example.medicalapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.medicalapp.R;
import com.example.medicalapp.model.Appointment;
import com.example.medicalapp.helper.DatabaseHelper;
import java.util.List;

public class AppointmentAdapter extends RecyclerView.Adapter<AppointmentAdapter.AppointmentViewHolder> {

    private List<Appointment> appointmentList;
    private DatabaseHelper dbHelper;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Appointment appointment);
    }

    public AppointmentAdapter(List<Appointment> appointmentList, DatabaseHelper dbHelper, OnItemClickListener listener) {
        this.appointmentList = appointmentList;
        this.dbHelper = dbHelper;
        this.listener = listener;
    }

    @NonNull
    @Override
    public AppointmentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_appointment, parent, false);
        return new AppointmentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AppointmentViewHolder holder, int position) {
        Appointment appointment = appointmentList.get(position);

        // 获取科室名称
        String deptName = dbHelper.getDepartmentNameById(appointment.getDeptId());

        holder.tvDeptName.setText(deptName);
        holder.tvAppointTime.setText("就诊时间: " + appointment.getAppointTime());

        // 设置状态标签
        String status = appointment.getStatus();
        holder.tvStatus.setText(status.equals("pending") ? "待就诊" :
                status.equals("completed") ? "已完成" : "已取消");

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(appointment);
            }
        });
    }

    @Override
    public int getItemCount() {
        return appointmentList.size();
    }

    public static class AppointmentViewHolder extends RecyclerView.ViewHolder {
        TextView tvDeptName;
        TextView tvAppointTime;
        TextView tvStatus;

        public AppointmentViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeptName = itemView.findViewById(R.id.tv_dept_name);
            tvAppointTime = itemView.findViewById(R.id.tv_appoint_time);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }
    }
}